package com.zh.note.huawei.twohundred.字符串划分;

/**
 * @Author: zhanghuan
 * @date: 2023/8/25 11:11
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // String s = sc.nextLine();
        String s = "aabaacaa";
        int[] res = findIndex(s);
        System.out.println(res[0] + "," + res[1]);


    }

    private static int[] findIndex(String s) {
        char[] chars = s.toCharArray();
        int sum = 0;
        for (char ch : chars) {
            sum += ch;
        }
        int sumStart = (sum - 'z' * 2) / 3;
        int sumEnd = (sum - 'a' * 2) / 3;
        if (sumStart < 0 || sumEnd < 0) {
            return new int[]{0, 0};
        }
        int leftSum = 0;
        int i = 0;
        int[] res = new int[]{0, 0};
        while (leftSum <= sumEnd) {
            leftSum += chars[i];
            i++;
            if (leftSum >= sumStart) {
                int rightSum = 0;
                int j = chars.length - 1;
                while (rightSum < sumEnd) {
                    rightSum += chars[j];
                    j--;
                    if (rightSum == leftSum && i < j) {
                        res = new int[]{i, j};
                        break;//这里已经找到了，但是break只能跳出当前的while,外层的循环其实没有必要了，所以外面再判断一次
                    }

                }
                if (rightSum == leftSum && i < j) {
                    break;
                }
            }
        }
        return res;
    }
}
